#ifndef __MINIKIT_SAFE_PACK_H__
#define __MINIKIT_SAFE_PACK_H__
#include <stdarg.h>
#include <stdint.h>
#include <string>
#include "../mini.kit/text.h"

namespace MINIKIT
{

class SafePackException : public std::exception
{
public:
	SafePackException( const std::string& msg  );
	SafePackException( const char* fmt, ...  );
	virtual ~SafePackException() throw( );
	virtual const char* what() const throw( );
protected:
	std::string m_message;
};//class SafePackException

class SafePack
{
public:
	SafePack( );
	void attach( char* data_buf, uint32_t data_len, int8_t net_endian=1 );
	void detach( );
	uint32_t getProcessLength( );
	uint32_t getRemainLength( );
	static uint64_t ntohl64( uint64_t val );
	static uint64_t htonl64( uint64_t val );
	
protected:
	int8_t   m_net_endian;
	char*    m_data_buf;
	uint32_t m_data_len;
	uint32_t m_left_len;

};//class SafePack

class SafePackGetter: public SafePack
{
public:
	SafePackGetter( );
	void get( int8_t  &value, const std::string &val_comment="" );
	void get( uint8_t &value, const std::string &val_comment="" );
	void get( int16_t &value, const std::string &val_comment="" );
	void get( uint16_t &value, const std::string &val_comment="" );
	void get( int32_t &value, const std::string &val_comment="" );
	void get( uint32_t &value, const std::string &val_comment="" );
	void get( int64_t &value, const std::string &val_comment="" );
	void get( uint64_t &value, const std::string &val_comment="" );
	void get( std::string &value, const std::string &val_comment="" );
};//class SafePackGetter

class SafePackSetter: public SafePack
{
public:
	SafePackSetter( );
	void set( int8_t   value, const std::string &val_comment="" );
	void set( uint8_t  value, const std::string &val_comment="" );
	void set( int16_t  value, const std::string &val_comment="" );
	void set( uint16_t value, const std::string &val_comment="" );
	void set( int32_t  value, const std::string &val_comment="" );
	void set( uint32_t value, const std::string &val_comment="" );
	void set( int64_t  value, const std::string &val_comment="" );
	void set( uint64_t value, const std::string &val_comment="" );
	void set( const std::string &value, const std::string &val_comment="" );
};//class SafePackSetter


};//namespace MINIKIT

#endif /*__MINIKIT_SAFE_PACK_H__*/

